//
//
//
//
//  Created by lbxia on 15/10/21.
//  Copyright © 2015年 lbxia. All rights reserved.
//

#import "SubLBXScanViewController.h"
#import "LBXAlertAction.h"
#import "GMButton.h"
#import "GMAddSNCarViewController.h"

@interface SubLBXScanViewController ()

@property (nonatomic, strong)GMButton *snButton;
@end

@implementation SubLBXScanViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupMainView];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    [self.view bringSubviewToFront:self.snButton];
}
- (void)setupMainView {
    self.navigationItem.title = @"添加车辆";

    self.view.backgroundColor = [UIColor blackColor];
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {

        self.edgesForExtendedLayout = UIRectEdgeNone;
    }

    [self.view addSubview:self.snButton];
    [self.snButton autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:52];
    [self.snButton autoAlignAxisToSuperviewAxis:ALAxisVertical];
    [self.snButton autoSetDimensionsToSize:CGSizeMake(240, 44)];
}

#pragma mark - Getter for snButton
- (GMButton *)snButton {
    if (!_snButton) {
        _snButton = [[GMButton alloc] init];
        [_snButton setTitle:@"点我手动输入二维码" forState:UIControlStateNormal];
        [_snButton addTarget:self action:@selector(snButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    }

    return _snButton;
}

#pragma mark - snButtonPressed
- (void)snButtonPressed {
    GMAddSNCarViewController *addSNCarViewController = [[GMAddSNCarViewController alloc] init];
    [self.navigationController pushViewController:addSNCarViewController animated:YES];
}

#pragma mark - Scan result
- (void)showError:(NSString*)str
{
    [LBXAlertAction showAlertWithTitle:@"提示" msg:str chooseBlock:nil buttonsStatement:@"知道了",nil];
//    NSLog(@"Error: %@", str);
}

- (void)scanResultWithArray:(NSArray<LBXScanResult*>*)array
{
    
    if (array.count < 1)
    {
        [self showScanResult:nil];
     
        return;
    }
    
    //经测试，可以同时识别2个二维码，不能同时识别二维码和条形码
    for (LBXScanResult *result in array) {
        
        NSLog(@"scanResult:%@",result.strScanned);
    }
    
    LBXScanResult *scanResult = array[0];
    
    NSString*strResult = scanResult.strScanned;
    
    self.scanImage = scanResult.imgScanned;
    
    if (!strResult) {

        [self showScanResult:nil];
        
        return;
    }
    
    //震动提醒
    [LBXScanWrapper systemVibrate];
    //声音提醒
    [LBXScanWrapper systemSound];
    [self showScanResult:strResult];
   
}

- (void)showScanResult:(NSString*)strResult
{
    if (!strResult) {
        
        strResult = @"识别失败";
    }

    __weak __typeof(self) weakSelf = self;
    [LBXAlertAction showAlertWithTitle:@"扫码内容" msg:strResult chooseBlock:^(NSInteger buttonIdx) {

        //点击完，继续扫码
        [weakSelf reStartDevice];
    } buttonsStatement:@"知道了",nil];
}

@end
